//index.js
//获取应用实例
const app = getApp()
var common = require('../../utils/common.js');
Page({
  data: {
    getUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    needInviteCode: false,
    inviteCode: '',
    step2: false,
    step1: true
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '京多多',
      path: '/pages/index/index?userId=' + app.globalData.userId,
      imageUrl: '/img/sharepic.jpg'
    }
  },
  onLoad: function () {
    if(app.globalData.transferuserId !== undefined){
      this.setData({
        transferuserId: app.globalData.transferuserId
      })
    }
    app.globalData.newuser = true
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  onGotUserInfo: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
    if (e.detail.userInfo){
      var res = e.detail
      getApp().globalData.userInfo = e.detail.userInfo
      typeof cb == "function" && cb(that.globalData.userInfo)
      // 可以将 res 发送给后台解码出 unionId
      console.log(res)
      getApp().globalData.userInfo = res.userInfo
      getApp().globalData.rawData = res.rawData
      getApp().globalData.signature = res.signature
      getApp().globalData.encryptedData = res.encryptedData
      getApp().globalData.iv = res.iv
      var transferuserId, transferskuId
      if (app.globalData.transferuserId){
        transferuserId = app.globalData.transferuserId
      }else{
        transferuserId = ''
      }
      if (app.globalData.transferskuId) {
        transferskuId = app.globalData.transferskuId
        app.globalData.isdetail = true
      } else {
        transferskuId = ''
      }
      const requestTask = wx.request({
        method: "POST",
        header: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          rawData: res.rawData,
          signature: res.signature,
          encryptedData: res.encryptedData,
          iv: res.iv,
          sessionId: app.globalData.jsessionId,
          invitedUserId: transferuserId,
          invitedSkuId: transferskuId,
          channelCode: app.globalData.channel || '',
          scene: getApp().globalData.scene || ''
        },
        url: getApp().globalData.url + "/wxa/user/updateInfo",
        async: true,
        timeout: 10000,
        success: function (data) {
          console.log(data)
          if (data.data.user.openId) {
            getApp().globalData.openId = data.data.user.openId
          }
          getApp().globalData.sessionId = data.data.user.sessionId
          getApp().globalData.sharelink = data.data.user.inviteLink
          getApp().globalData.userId = data.data.user.userId
          getApp().globalData.spreader = data.data.user.isSpreader
          getApp().globalData.userInfo = data.data.user
          
          wx.setStorage({
            key: "sessionId",
            data: data.data.user.sessionId
          })
          wx.setStorage({
            key: "sharelink",
            data: data.data.user.inviteLink
          })
          wx.setStorage({
            key: "userId",
            data: data.data.user.userId
          })
          wx.setStorage({
            key: "spreader",
            data: data.data.user.isSpreader
          })
          wx.setStorage({
            key: "userInfo",
            data: data.data.user
          })
          console.log(getApp().globalData.item)
          console.log(getApp().globalData.fromactive)
          if(data.data.user.needInviteCode){
            wx.redirectTo({
              url: '/pages/needCode/needCode'
            })
          }else{
            if(app.globalData.thispath !== ''){
              wx.reLaunch({
                url: app.globalData.thispath,
                success(res){
                  console.log(res)
                },
                fail(res) {
                  console.log(res)
                }
              })
            }else{
              wx.reLaunch({
                url: app.globalData.path,
                success(res){
                  console.log(res)
                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          }
          
          
        },
        error: function (textStatus) {
          console.error(textStatus);
        },
        complete: function (XMLHttpRequest, status) {
          if (status == 'timeout') {
            requestTask.abort()    // 超时后中断请求
            wx.showModal({
              title: '提示',
              content: '网络超时，请重试',
              showCancel: 'false',
              success: function (res) {
                if (res.confirm) {
                  app.login()
                }
              }
            })
          }
        }
      })
    }
  },
  inviteCode(){
    const that = this
    common.post({
      url: '/app/user/inviteCode', data: { inviteCode: this.data.inviteCode, sessionId: app.globalData.sessionId }
    })
      .then((res) => {
        console.log(res.data)
        if(res.data.code == 0){
          if(app.globalData.thispath !== ''){
            wx.reLaunch({
              url: app.globalData.thispath,
              success(res){
                console.log(res)
              },
              fail(res) {
                console.log(res)
              }
            })
          }else{
            wx.reLaunch({
              url: app.globalData.path,
              success(res){
                console.log(res)
              },
              fail(res) {
                console.log(res)
              }
            })
          }
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  inputWacth: function (e) {
    console.log(e);
    this.setData({
      inviteCode: e.detail.value
    });
  },
})
